/*
 * Multicast Client
 * Project 3 
 * Created by: Wilson Burhan & Derrick Mink
 */

import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
 * The multicast client program that displays all the communication sent by the unicast client.
 * @author Wilson Burhan & Derrick Mink
 */
public class mClient {
	public static void main(String[] args) throws IOException{
	    Scanner scan = new Scanner(System.in);
		System.out.print("Please enter the multicast address: ");
		String host = scan.next();
	    System.out.print("Please enter the port number: ");
	    int port = scan.nextInt();
	    
		try{
			MulticastSocket mSocket = new MulticastSocket(port);
			InetAddress mAddress = InetAddress.getByName(host);
			mSocket.joinGroup(mAddress);
			byte [] buffer = new byte[1024];
			while(true) {
				DatagramPacket receiveData = new DatagramPacket(buffer, buffer.length);
				mSocket.receive(receiveData);
				String s = new String(receiveData.getData(), 0, receiveData.getLength());
					
				System.out.println("Client"+ receiveData.getAddress()+": " + s);
			}
		}
		catch(SocketException se) {
			System.out.println("Socket Exception : " + se); }
		catch (IOException e) { 
			System.out.println("Exception : " + e); 
		}
	}
}


